#!/usr/bin/env bash

source "$rvm_scripts_path/functions/db"

__rvm_autolibs_set()
{
  __rvm_db_ "$rvm_user_path/db" "autolibs" "$rvm_autolibs_flag"
}

__rvm_autolibs_get()
{
  if [[ -z "${rvm_autolibs_flag:-}" ]]
  then rvm_autolibs_flag="$( __rvm_db_ "$rvm_user_path/db" "autolibs" )"
  fi

  rvm_autolibs_flag="${rvm_autolibs_flag:=4}"
}

__rvm_autolibs_reset()
{
  __rvm_db_ "$rvm_user_path/db" "autolibs" "delete"
}

__rvm_autolibs_translate()
{
  case "${1:-$rvm_autolibs_flag}" in
    (0|disable|disabled)
      rvm_autolibs_flag_number=0
      rvm_autolibs_flag_runner="${_system_name_lowercase}"
      ;;
    (1|read|read-only)
      rvm_autolibs_flag_number=1
      rvm_autolibs_flag_runner="${_system_name_lowercase}"
      ;;
    (2|fail|read-fail)
      rvm_autolibs_flag_number=2
      rvm_autolibs_flag_runner="${_system_name_lowercase}"
      ;;
    (3|packages|install-packages)
      rvm_autolibs_flag_number=3
      rvm_autolibs_flag_runner="${_system_name_lowercase}"
      ;;
    (portage|gentoo_portage)
      rvm_autolibs_flag_number=3
      rvm_autolibs_flag_runner="gentoo_portage"
      ;;
    (paludis|gentoo_paludis)
      rvm_autolibs_flag_number=3
      rvm_autolibs_flag_runner="gentoo_paludis"
      ;;
    (pkgcore|gentoo_pkgcore)
      rvm_autolibs_flag_number=3
      rvm_autolibs_flag_runner="gentoo_pkgcore"
      ;;
    (4|enable|enabled)
      rvm_autolibs_flag_number=4
      rvm_autolibs_flag_runner="${_system_name_lowercase}"
      ;;
    (brew|homebrew|osx_brew)
      rvm_autolibs_flag_number=4
      rvm_autolibs_flag_runner="osx_brew"
      ;;
    (port|macports|osx_port)
      rvm_autolibs_flag_number=4
      rvm_autolibs_flag_runner="osx_port"
      ;;
    (fink|osx_fink)
      rvm_autolibs_flag_number=4
      rvm_autolibs_flag_runner="osx_fink"
      ;;
    (smf|rvm_pkg)
      rvm_autolibs_flag_number=4
      rvm_autolibs_flag_runner="${rvm_autolibs_flag}"
      ;;
    (*)
      rvm_debug "Unknown 'rvm_autolibs_flag' value '$rvm_autolibs_flag'."
      return 1
      ;;
  esac

  if [[ -n "${1:-}" ]]
  then rvm_autolibs_flag="$1"
  fi
}

__rvm_autolibs_translate_description()
{
  __rvm_autolibs_translate

  case "${rvm_autolibs_flag_number}" in
    (0)
      rvm_autolibs_flag_description="Limit RVM automation as much as possible."
      ;;
    (1)
      rvm_autolibs_flag_description="Allow RVM to use package manager if found but do not install or fail if dependencies are missing."
      ;;
    (2)
      rvm_autolibs_flag_description="Allow RVM to use package manager if found, fail if dependencies are missing. This is default."
      ;;
    (3)
      rvm_autolibs_flag_description="Allow RVM to use package manager if found, install missing dependencies."
      ;;
    (4)
      rvm_autolibs_flag_description="Allow RVM to use package manager if found, install missing dependencies, install package manager (only OS X)."
      ;;
  esac
}
